package com.itheimaoa.web.action.customer;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.itheimaoa.domain.customer.Customer;
import com.itheimaoa.service.customer.ICustomerService;
import com.itheimaoa.web.form.customer.CustomerForm;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
public class FrontCustomerAction extends ActionSupport implements ModelDriven<CustomerForm> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource
	private ICustomerService customerService;

	public String reg() {
		return "reg";
	}

	public String doReg() {
		Customer customer = new Customer();
		customer.setAddress(form.getAddress());
		customer.setCellphone(form.getCellphone());
		customer.setCustomerStatus(form.getCustomerStatus());
		customer.setEmail(form.getEmail());
		customer.setGender(form.getGender());
		customer.setInfoSource(form.getInfoSource());
		customer.setIntentionCourse(form.getIntentionCourse());
		customer.setMessage(form.getMessage());
		customer.setName(form.getName());
		customer.setQq(form.getQq());
		customerService.saveEntity(customer);
		form.setMessage("注册成功,马上登录!");
		ActionContext.getContext().put("urladdress", "itheimaoa/customer/login.do");
		return "message";
	}

	public String login() {
		return "login";
	}

	public String doLogin() {
		Customer customer = customerService.checkLogin(form.getName(), form.getCellphone());
		if (customer != null) {
			ServletActionContext.getRequest().getSession().setAttribute("customer", customer);
			return "action2apply";
		}
		return "login";
	}

	private CustomerForm form = new CustomerForm();

	@Override
	public CustomerForm getModel() {
		return form;
	}
}
